<?

class Login { // class start

    private $nmg_table;
    private $facebook_table;
    private $weibo_table;
    private $openid_table;
    private $table;
    private $db;

    public function __construct() {
        
    }

    /** facebook login
     * @param  String	$redirect_uri	return url
     * @param  int		$bind			1:綁定 0:不綁定  //denis 修改 bind默認為0
     * @return Array
     */
    public function login_facebook($redirect_uri = '', $bind = 0) {
        require_once(LIB_PATH . '/Vendor/Facebook/sdk/facebook.php');
        require_once(LIB_PATH . '/Vendor/Facebook/FaceBookApi.class.php');

        $fb_config = array();
        $fb_config['appId'] = FB_APPID;
        $fb_config['secret'] = FB_SECRET;
        $fb_config['scope'] = FB_SCOPE;
        $fb_config['redirect_uri'] = isset($redirect_uri) ? $redirect_uri : FB_REDIRECT_URI;
        $fb_config['cookie'] = FB_COOKIE;
        $fb_config['fileUpload'] = FB_FILEUPLOAD_SUPPORT;


        $facebook = new FaceBookApi($fb_config);
        $user = $facebook->getUser(0);

        if ($user) {
            $facebook_accesstoken = $facebook->getAccessToken();
            $result = $facebook->getUserInfo($user, 'uid,name,pic,pic_square,email,sex,birthday_date', $facebook_accesstoken);

            $ret = $this->check_fackbook_id_login($result, $facebook_accesstoken);

            return $this->response($ret[0], $ret[1], $ret[2], $ret[3]);
        } else {

            $is_facebookData = $_COOKIE["is_facebookData"];
            if (!$is_facebookData) {
                setcookie("is_facebookData", 1, time() + 100);
                $facebook->getLoginUrl(1); # go to facebook login
            } else {
                return $this->response(false, '012');
            }

            /*
              $sess_obj = new Session();
              if($sess_obj->get('LINKTOFB')==''){
              $sess_obj->set('LINKTOFB',1);
              $facebook->getLoginUrl(1);	# go to facebook login
              }else{
              $sess_obj->set('LINKTOFB','');
              return $this->response(false,'012');
              }
             */
        }
    }

    public function check_fackbook_id_login($result, $facebook_accesstoken) {
        $where = "TB1.`fb_id`=" . $result['uid'];
        $param = array();
        $param['field'] = "TB1.*,TB2.photo,TB2.username ,TB2.email,TB2.member_type,TB2.logintime";
        $param['where'] = $where;
        $param['order'] = ""; //"id asc"
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "LEFT JOIN member_imore";
        $param['on'] = "TB1.mid = TB2.id";

        $logindata = db_query_hash("member_facebook", $param);

        $logindata = $logindata['result'];
        /*
          $output = var_export($logindata,1);
          echo '<pre>'.$output.'</pre>';
          exit();
         */
        $param = array();
        $return = array();
        $where = "email='" . $result['email'] . "'";
        $param['field'] = "";
        $param['where'] = $where;
        $param['order'] = ""; //"id asc"
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "";
        $param['on'] = "";
        $memberData = db_query_hash("member_imore", $param);

        if ($logindata) {
            if ($logindata[0]['mid'] == 0) {

                if ($memberData['result']) {
                    $up_data = array();
                    $up_data['accesstoken'] = $facebook_accesstoken;
                    $up_data['mid'] = $memberData['result'][0]['id'];

                    $condition = "fb_id='" . $logindata[0]['fb_id'] . "'";
                    db_update("member_facebook", $up_data, $condition);
                    if ($this->checkban($memberData['result'][0]['id']) == false) {
                        $return = array(false, '禁止登錄', $memberData['result'][0], $result);
                    } else {
                        $return = array(true, '正常', $memberData['result'][0], $result);
                    }
                } else {
                    $return = array(true, '需要激活', array(), $result);
                }
            } else {
                $up_data = array();
                $up_data['accesstoken'] = $facebook_accesstoken;

                $condition = "fb_id='" . $logindata[0]['fb_id'] . "'";
                db_update("member_facebook", $up_data, $condition);
                if ($this->checkban($logindata[0]['mid']) == false) {
                    $return = array(false, '禁止登錄', $memberData['result'][0], $result);
                } else {
                    $return = array(true, '正常', $memberData['result'][0], $result);
                }
            }
        } else {
            if ($memberData['result']) {

                $in_data = array();
                $in_data['fb_id'] = $result['uid'];
                $in_data['mid'] = $memberData['result'][0]['id'];
                $in_data['accesstoken'] = $facebook_accesstoken;
                $in_data['regtime'] = date('Y-m-d H:i:s', time());
                $in_data['regip'] = getip();
                $in_data['logintime'] = date('Y-m-d H:i:s', time());
                $newid = db_add("member_facebook", $in_data); # insert to facebook

                if ($this->checkban($memberData['result'][0]['id']) == false) {
                    $return = array(false, '禁止登錄', $memberData['result'][0], $result);
                } else {
                    $return = array(true, '正常', $memberData['result'][0], $result);
                }
            } else {
                $in_data = array();
                $in_data['fb_id'] = $result['uid'];
                $in_data['mid'] = 0;
                $in_data['accesstoken'] = $facebook_accesstoken;
                $in_data['regtime'] = date('Y-m-d H:i:s', time());
                $in_data['regip'] = getip();
                $in_data['logintime'] = date('Y-m-d H:i:s', time());
                $newid = db_add("member_facebook", $in_data); # insert to facebook
                $return = array(true, '需要激活', array(), $result);
            }
        }

        return $return;
    }

    /** 檢測用戶是否被禁止
     * @param	int	$mid	用戶id
     * @return	boolean
     */
    private function checkban($mid) {
        if ($mid == 0) {
            return true;
        }
        $member = db_query_count("member_imore", "id='" . $mid . "' and status<>1");
        if ($member) {
            return false; # 被禁止
        } else {
            return true; # 可以使用
        }
    }

    private function exists($obj) {
        return isset($obj) && !empty($obj);
    }

    private function response($status = '', $info = '', $memberData = array(), $data = array()) {
        return array($status, $info, $memberData, $data);
    }

}

// class end
?>